package com.example.itemdecorationdemo.fragment

import android.graphics.Rect
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.itemdecorationdemo.R
import com.example.itemdecorationdemo.itemDecoration.PinnedSectionDecoration
import com.example.itemdecorationdemo.itemDecoration.PinnedSectionDecorationHead
import com.example.itemdecorationdemo.itemDecoration.PinnedSectionTestDecoration
import com.example.itemdecorationdemo.itemDecoration.TestItemDecoration

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private const val ARG_PARAM1 = "param1"
private const val ARG_PARAM2 = "param2"

/**
 * A simple [Fragment] subclass.
 * Use the [GridTestFragment.newInstance] factory method to
 * create an instance of this fragment.
 */
class GridTestFragment : Fragment() {
    private var param1: String? = null
    private var param2: String? = null

    private lateinit var recyclerView: RecyclerView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            param1 = it.getString(ARG_PARAM1)
            param2 = it.getString(ARG_PARAM2)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_grid_test, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        recyclerView = view.findViewById(R.id.fragment_grid_test_rv)
        var layoutManager = GridLayoutManager(context, 2)


        Log.d(
            "zhangyu",
            "onViewCreated ${arguments?.getInt("type")}"
        )
        when (arguments?.getInt("type") ?: 0) {
            100 -> {
                layoutManager = GridLayoutManager(context, 2)
                layoutManager.orientation = GridLayoutManager.VERTICAL
            }
            101 -> {
                layoutManager = GridLayoutManager(context, 2)
                layoutManager.orientation = GridLayoutManager.HORIZONTAL
            }
            102 -> {
                recyclerView.addItemDecoration(SimpleDecoration())
            }
            103 -> {
                recyclerView.addItemDecoration(SimpleDecoration())
            }

            else -> {

            }
        }

        recyclerView.layoutManager = layoutManager
        recyclerView.adapter = object : RecyclerView.Adapter<ViewHolder>() {
            override fun onCreateViewHolder(
                parent: ViewGroup,
                viewType: Int
            ): ViewHolder {
                val view = LayoutInflater.from(parent.context)
                    .inflate(R.layout.grid_fragment_item, parent, false)
                return ViewHolder(view)
            }

            override fun onBindViewHolder(holder: ViewHolder, position: Int) {
                holder.title.text = "第" + position + "个item"
            }

            override fun getItemCount(): Int {
                return 101
            }
        }
    }

    class SimpleDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            //outRect.top = 10
            Log.d("zhangyu1016", "SimpleDecoration")
            outRect[20, 40, 5] = 10
        }
    }

    private class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var title: TextView = itemView.findViewById(R.id.grid_fragment_item_title)

    }

    companion object {
        @JvmStatic
        fun newInstance(param1: String, param2: String) =
            GridTestFragment().apply {
                arguments = Bundle().apply {
                    putString(ARG_PARAM1, param1)
                    putString(ARG_PARAM2, param2)
                }
            }
    }
}